package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.AResult;
import com.ruoyi.system.mapper.AResultMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.AOutboundRecordMapper;
import com.ruoyi.system.domain.AOutboundRecord;
import com.ruoyi.system.service.IAOutboundRecordService;

/**
 * 成品出库Service业务层处理
 *
 */
@Service
public class AOutboundRecordServiceImpl implements IAOutboundRecordService 
{
    @Autowired
    private AOutboundRecordMapper aOutboundRecordMapper;

    @Autowired
    private AResultMapper aResultMapper;

    /**
     * 查询成品出库
     * 
     * @param id 成品出库主键
     * @return 成品出库
     */
    @Override
    public AOutboundRecord selectAOutboundRecordById(Long id)
    {
        return aOutboundRecordMapper.selectAOutboundRecordById(id);
    }

    /**
     * 查询成品出库列表
     * 
     * @param aOutboundRecord 成品出库
     * @return 成品出库
     */
    @Override
    public List<AOutboundRecord> selectAOutboundRecordList(AOutboundRecord aOutboundRecord)
    {
        return aOutboundRecordMapper.selectAOutboundRecordList(aOutboundRecord);
    }

    /**
     * 新增成品出库
     * 
     * @param aOutboundRecord 成品出库
     * @return 结果
     */
    @Override
    public int insertAOutboundRecord(AOutboundRecord aOutboundRecord)
    {
        int i = 0;
        Long deptId = SecurityUtils.getLoginUser().getDeptId();
        aOutboundRecord.setGroupId(deptId.toString());
        String[] boxNumbers = aOutboundRecord.getBoxNumber().trim().split("\\n");
        for (String boxNumber : boxNumbers) {
            aOutboundRecord.setBatchNumber(boxNumber);
            AResult aResult  = new AResult();
            aResult.setBoxNo(boxNumber);
            List<AResult> aResults =  aResultMapper.selectAResultList(aResult);
            if (aResults.size() > 0){

                Double boxQty = aResultMapper.getBoxQty(boxNumber);
                Long roundedValue = Math.round(boxQty);
                AResult aResult1 = aResults.get(0);
                aOutboundRecord.setBatchNumber(aResult1.getInBatchNo()); //入库批次号
                aOutboundRecord.setProductionRecord(aResult1.getProductionOrder());  //工单号
                aOutboundRecord.setPalletNumber(aResult1.getPalletNo());//拖号
                aOutboundRecord.setQuantiyt(String.valueOf(roundedValue));//数量
                aOutboundRecord.setUnit(aResult1.getUnit()); //单位
                aOutboundRecord.setColor(aResult1.getColor());//颜色
                aOutboundRecord.setPressure(aResult1.getPressure());//开压
                aOutboundRecord.setEfficiency(aResult1.getEfficiency());//效率
                aOutboundRecord.setPower(aResult1.getPower());//功率
                aOutboundRecord.setGrade(aResult1.getGrade());//等级
                aOutboundRecord.setDescription(aResult1.getProductDesc());//描述
                aOutboundRecord.setBoxNumber(boxNumber);
                i = i + aOutboundRecordMapper.insertAOutboundRecord(aOutboundRecord);
            }else {
                throw new RuntimeException("未获取到箱信息！");
            }

        }

        return i;
    }

    /**
     * 修改成品出库
     * 
     * @param aOutboundRecord 成品出库
     * @return 结果
     */
    @Override
    public int updateAOutboundRecord(AOutboundRecord aOutboundRecord)
    {
        return aOutboundRecordMapper.updateAOutboundRecord(aOutboundRecord);
    }

    /**
     * 批量删除成品出库
     * 
     * @param ids 需要删除的成品出库主键
     * @return 结果
     */
    @Override
    public int deleteAOutboundRecordByIds(Long[] ids)
    {
        return aOutboundRecordMapper.deleteAOutboundRecordByIds(ids);
    }

    /**
     * 删除成品出库信息
     * 
     * @param id 成品出库主键
     * @return 结果
     */
    @Override
    public int deleteAOutboundRecordById(Long id)
    {
        return aOutboundRecordMapper.deleteAOutboundRecordById(id);
    }
}
